home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / chrcount.bat < prev    next >
DOS Batch File  |  1995-04-07  |  2KB  |  71 lines

  1. @ECHO OFF
  2. REM *************************************************************
  3. REM *** ChrCount.bat - Count the number off occurences of any ***
  4. REM *** ver.1          character in a file.                   ***
  5. REM *************************************************************
  6.  
  7. CEnviD %0.bat %1 %2 %3 %4
  8. GOTO CENVI_EXIT
  9.  
  10. Instructions()
  11. {
  12.    puts("CHRCOUNT.BAT - Count occurences of a character in a file; save in CHRCOUNT\a")
  13.    puts(``)
  14.    puts(`USAGE: CHRCOUNT <filespec> <[x]character>`)
  15.    puts(`Where: filespec  - Name of file to read`)
  16.    puts(`       character - Character to search for, if "X" first then`)
  17.    puts(`                   character is a hexidecimal value`)
  18.    puts(``)
  19.    puts(`NOTE: Character count is saved in the CHRCOUNT environment variable`)
  20.    puts(``)
  21.    puts(`Examples: CHRCOUNT C:\AUTOEXEC.BAT :`)
  22.    puts(`          CHRCOUNT C:\AUTOEXEC.BAT A`)
  23.    puts(`          CHRCOUNT C:\AUTOEXEC.BAT xA`)
  24.    puts(``)
  25.    exit(EXIT_FAILURE);
  26. }
  27.  
  28. main(argc,argv)
  29. {
  30.    if ( argc != 3 )
  31.       Instructions();
  32.  
  33.    // Get the character they're looking for
  34.    if ( argv[2][1]  &&  'X' == toupper(argv[2][0]) )
  35.       Character = strtol(argv[2]+1,end,16);
  36.    else if ( !argv[2][1] )
  37.       Character = argv[2][0];
  38.    else {
  39.       printf("\aI don't understand the character \"%s\".\n",argv[2]);
  40.       exit(EXIT_FAILURE);
  41.    }
  42.  
  43.    // open and read file
  44.    fp = fopen(argv[1],"rb");
  45.    if ( !fp ) {
  46.       printf("\aUnable to open \"%s\" for reading.\n",argv[1]);
  47.       exit(EXIT_FAILURE);
  48.    }
  49.  
  50.    // finally, read in file and search for character
  51.    CHRCOUNT = CountCharacterInFile(fp,Character);
  52.    printf("Character found %d times.\n",CHRCOUNT);
  53.    fclose(fp);
  54. }
  55.  
  56. CountCharacterInFile(fp,c)
  57. {
  58.    Total = 0;
  59.    while ( Read = fread(buf,4000,fp) ) {
  60.       for ( size = 0; size < Read; size++ ) {
  61.          if ( !(found = memchr(buf+size,c,Read-size)) )
  62.             break;
  63.          Total++;
  64.          size = found - buf;
  65.       }
  66.    }
  67.    return Total;
  68. }
  69.  
  70. :CENVI_EXIT
  71.